import datetime
from dataclasses import dataclass

from src.application.user import dto
from src.application.user.converters import convert_active_user_entity_to_dto
from src.application.user.uow import UserUoW
from src.domain.user.value_objects import TelegramUserId


@dataclass(frozen=True)
class SetTestProxyAcquired:
    tg_user_id: int
    test_proxy_acquired: datetime.datetime


class SetTestProxyAcquiredHandler:
    """
    Обновление test proxy acquired юзера
    """

    def __init__(self, *, uow: UserUoW) -> None:
        self._uow = uow

    async def __call__(self, command: SetTestProxyAcquired) -> dto.UserDTO:
        user_id = TelegramUserId(value=command.tg_user_id)
        user = await self._uow.user_repo.acquire_user_by_id(user_tg_id=user_id)

        user.set_test_proxy_acquired(
            test_proxy_acquired=command.test_proxy_acquired,
        )
        await self._uow.user_repo.update_user(user=user)
        await self._uow.commit()

        user_dto = convert_active_user_entity_to_dto(user)

        return user_dto
